/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "ITransformable.h"
#include "core/core.h"

// TODO:
// wenn man versucht z.B. die Transformation einer IStaticTransformable 
// uber eine Matrix zu setzen wird anscheined die z-Richtung vertauscht.
// das passiert aber nicht, wenn die Translation ueber setPosition (Vector3) 
// durchgefuehrt wird...


ITransformable::ITransformable(void)
:	IResource(),
	m_transformType(UNDEFINED_TRANSFORM)
{
}

ITransformable::~ITransformable(void)
{
}

eTransformType ITransformable::getTransformType() const
{
	return m_transformType;
}

DynamicTransform::DynamicTransform(void)
: ITransformable()
{
	m_transformType = DYNAMIC_TRANSFORM;
	qRotation.reset();
	vPosition.setZero();
	vScale = math::Vector3(1.0f, 1.0f, 1.0f);
}

DynamicTransform::~DynamicTransform(void)
{
}

void DynamicTransform::setPosition(const math::Vector3 &position)
{
	vPosition = position;
}

void DynamicTransform::setPosition(const float &x, const float &y, const float &z) {
	vPosition.x = x;
	vPosition.y = y;
	vPosition.z = z;
}

void DynamicTransform::setScale(const math::Vector3 &scale)
{
	vScale = scale;
}

void DynamicTransform::setRotation(const math::Quaternion &quaternion)
{
	qRotation = quaternion;
}

const math::Vector3& DynamicTransform::getPosition() const
{
	return vPosition;
}

const math::Quaternion& DynamicTransform::getRotation() const
{
	return qRotation;
}

void DynamicTransform::setMatrix(const float* matrix)
{
	mat = math::Matrix4((float*)matrix);
	vPosition = mat.getTranslation();
	qRotation = mat.getQuaternion();
}

const math::Matrix4& DynamicTransform::getMatrix()
{
	mat = getRotation().getMatrix4();
	mat.setTranslation( vPosition );
	//mat.setScale( vScale );
	return mat;
}

void DynamicTransform::rotate(const math::Quaternion &quaternion)
{
	qRotation *= quaternion;
}

ITransformable* DynamicTransform::copy()
{
	DynamicTransform *transform = new DynamicTransform();
	transform->qRotation = this->qRotation;
	transform->vPosition = this->vPosition;
	transform->vScale = this->vScale;

	return transform;
}


StaticTransform::StaticTransform(void)
: ITransformable()
{
	m_transformType = STATIC_TRANSFORM;
}

StaticTransform::~StaticTransform(void)
{
}

void StaticTransform::setPosition(const math::Vector3 &position)
{
	mTransform.setTranslation( position );
}

void StaticTransform::setPosition(const float &x, const float &y, const float &z) {
	mTransform.setTranslation(x, y, z);
}

const math::Vector3 StaticTransform::getPosition() const
{
	math::Vector3 pos(mTransform[12] /  mTransform[15], mTransform[13] /  mTransform[15], mTransform[14] /  mTransform[15]);
	return pos;
}

const math::Vector3 StaticTransform::getScale() const
{
	math::Vector3 pos(mTransform[0] /  mTransform[15], mTransform[5] /  mTransform[15], mTransform[10] /  mTransform[15]);
	return pos;
}

void StaticTransform::setScale(const math::Vector3 &scale)
{
	mTransform.setScale( scale );
}

void StaticTransform::setRotation(const math::Quaternion &quaternion)
{
	math::Quaternion quat = quaternion;
	quat.normalize();
	math::Matrix4 mat = quat.getMatrix4();
	mat.setTranslation(mTransform.getTranslation());
	mTransform = mat;
}

void StaticTransform::addRotation(const math::Quaternion &quaternion){
	math::Quaternion quat = quaternion;
	quat.normalize();
	math::Matrix4 mat = quat.getMatrix4();
	mTransform = mat * mTransform;	
}

void StaticTransform::setMatrix(const math::Matrix4 &matrix)
{
	mTransform = matrix;
}

void StaticTransform::setMatrix(const float* matrix)
{
	mTransform = math::Matrix4((float*)matrix);
}

const math::Matrix4& StaticTransform::getMatrix()
{	
	return mTransform;
}

void StaticTransform::rotate(const math::Quaternion &quaternion)
{
	math::Matrix4 mat = quaternion.getMatrix4();
	mTransform *= mat;
}

ITransformable* StaticTransform::copy()
{
	StaticTransform *transform = new StaticTransform();
	transform->mTransform = this->mTransform;
	return transform;
}
